//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
const app = getApp()

Page({
    data: {
        goodsDetail: {},
        userInfo: {},
        logged: false,
        takeSession: false,
        requestResult: '',
        option:{},
    },
    onLoad: function (options) {
        var that = this;
        var pid = options.pid;
        var sence = options.sence;
        this.setData({
            pid:pid,
            sence:sence
        })

        var userInfoStorage = wx.getStorageSync('userInfo');

        if (!util.isNUll(userInfoStorage)) {
            app.globalData.userInfo = userInfoStorage;
            that.getUserinfo(app.globalData.userInfo, pid);
        } else {
            app.login(function (e) {
                that.getUserinfo(wx.getStorageSync('userInfo'), pid);
            });
        }
    },
        getUserinfo: function (data, pid) {
        var that = this;

        that.setData({
            userInfo: data,
            hasUserInfo: true
        });

        wx.request({
            url: config.service.productDetail,
            data: {
                pid: pid,
                userid: that.data.userInfo.userid,

            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log('sharelink---'+JSON.stringify(res));
                that.setData({
                    goodsDetail: res.data.data,
                });
                if (!util.isNUll(that.data.userInfo.userid)&&!util.isNUll(that.data.goodsDetail.has_coupon)&&that.data.goodsDetail.has_coupon == 1) {
                    wx.showToast({
                        title: '已经领取过啦',
                        duration: 2000
                    })

                    setTimeout(function () {
                        wx.hideToast();
                        wx.redirectTo({
                            url: '../goods-details/index?item=' + JSON.stringify(that.data.goodsDetail)
                        })
                    }, 2000);
                }


            }
        })
    },
    getCoupon: function (e) {
        var that = this;
        wx.request({
            url: config.service.couponAdd,
            data: {
                pid: that.data.pid,
                userid: that.data.userInfo.userid,
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {

                var data = that.data.goodsDetail;
                data.has_coupon = 1;
                that.setData({
                    goodsDetail: data
                });
                wx.showToast({
                    title: '领取成功',
                    icon: 'success',
                    duration: 2000
                })

                setTimeout(function () {
                    wx.hideToast();
                    wx.redirectTo({
                        url: '../goods-details/index?item=' + JSON.stringify(that.data.goodsDetail)
                    })
                }, 2000);
            }
        })
    },
})
